package cn.stronglink.assetmanage.business.check.view.checked;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.MainActivity;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.adapter.CheckOrderAdapter;
import cn.stronglink.assetmanage.business.check.model.CheckedAssetModel;
import cn.stronglink.assetmanage.business.check.presenter.CheckedAssetPresenter;
import cn.stronglink.assetmanage.business.check.view.detail.WaitCheckDetailFragment;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.index.base.BaseChildFragment;
import cn.stronglink.assetmanage.index.entity.PromissionEntity;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ToastUtil;

/**
 * 入库表单列表
 */
public class CheckedAssetFragment extends BaseChildFragment {
    protected FragmentActivity mActivity;
    String orderNo = null;
    private Unbinder unbinder;
    private CheckOrderAdapter orderAdapter;
    private CheckedAssetModel checkedAssetModel;
    private CheckedAssetView checkedAssetView;
    private CheckedAssetPresenter checkedAssetPresenter;
    //某个单资产列表
    private RecyclerView rv_checked_in_list;
    private LinearLayoutManager layoutManager1;
    private TextView txt_left_title;
    private TextView txt_title_right, txt_title_left, txt_main_title;
    private List<Asset> assetList;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity = (FragmentActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_checked_asset, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        initViewAndData(rootView);
        return rootView;
    }

    private void initViewAndData(View view) {
        try {
            txt_title_right = this.getActivity().findViewById(R.id.txt_right_title);
            txt_title_right.setVisibility(View.INVISIBLE);
            txt_title_left = getActivity().findViewById(R.id.txt_left_title);
            txt_title_left.setVisibility(View.VISIBLE);
            txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
            txt_main_title.setText("已入库");
            assetList = new ArrayList<>();
            orderAdapter = new CheckOrderAdapter(this.getActivity());
            rv_checked_in_list = view.findViewById(R.id.rv_checked_in_list);
            layoutManager1 = new LinearLayoutManager(this.getActivity(), LinearLayout.VERTICAL, false);
            rv_checked_in_list.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
            rv_checked_in_list.setLayoutManager(layoutManager1);
            rv_checked_in_list.setAdapter(orderAdapter);
            orderAdapter.setOnItemClickListener(new onRecyclerListener() {
                @Override
                public void onItemClicked(View v, int postion) {
                    if (assetList != null && assetList.size() > 0) {
                        MainActivity activity = (MainActivity) getActivity();
                        PromissionEntity pe = new PromissionEntity();
//                        pe.setFragment(new CheckedDetailFragment());
                        pe.setFragment(new WaitCheckDetailFragment());
                        pe.setPosition(MainActivity.FRAGMENT_CUSTOM_ID);
                        pe.setTitle("已入库");
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.CHECK_IN_DETAIL_NO, assetList.get(postion).getAsset_id_key());
                        pe.setBundle(bundle);
                        activity.onEventRegister(pe);
                    }
                }
            });
            checkedAssetModel = new CheckedAssetModel();
            checkedAssetView = new CheckedAssetView(this, view, orderAdapter);
            checkedAssetPresenter = new CheckedAssetPresenter(checkedAssetView, checkedAssetModel);

            txt_left_title = this.getActivity().findViewById(R.id.txt_left_title);
            txt_left_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.onBackPressed();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.toastShort("没有数据显示");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
                if (keycode == KeyEvent.KEYCODE_BACK) {
                    mActivity.onBackPressed();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (!isVisible) {
            return;
        }
        try {
            Bundle bundle = getArguments();
            if (bundle != null) {
                orderNo = bundle.getString(Constants.CHECK_IN_DETAIL_NO);
                if (orderNo != null && !"".equals(orderNo)) {
                    assetList = checkedAssetModel.getDataByOrderNo(orderNo);
                    checkedAssetView.setOrderData(assetList);
                    checkedAssetView.showAssetTitle(checkedAssetModel.getCheckByOrderNo(orderNo));
                    orderAdapter.notifyDatasChanged(assetList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
